<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="部门名称">
              <a-input v-model="queryParam.deptName" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.addDept.add()">新建</a-button>
      <a-button type="dashed" @click="tableOption">{{ optionAlertShow && '关闭' || '开启' }}提示</a-button>
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="remove">
            <remove-dept ref="removeDept" :deptIds="selectedRowKeys.join(',')" @ok="handleOk"/>
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      showPagination="auto"
    >
      <span slot="deptType" slot-scope="text">
        <a-tag :color="text | deptTypeColorFilter">{{ text | deptTypeTextFilter }}</a-tag>
      </span>
      <span slot="deptFather" slot-scope="text">
        <a-tag color="#108ee9">{{ text | deptFatherTextFilter }}</a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <remove-dept ref="removeDept" :deptIds="String(record.deptId)" @ok="handleOk"/>
        </template>
      </span>
    </s-table>
    <add-dept ref="addDept" :deptFathers="deptFathers" @ok="handleOk" />
    <edit-dept ref="editDept" :deptFathers="deptFathers" @ok="handleOk"/>
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '@/components'
import { queryDept, listDept } from '@/api/basic/dept'
import AddDept from './modules/dept/AddDept'
import EditDept from './modules/dept/EditDept'
import RemoveDept from './modules/dept/RemoveDept'
// 用于处理过滤器动态获取数据
let that

const deptTypeMap = {
  1: {
    color: 'orange',
    text: '一级单位'
  },
  2: {
    color: 'green',
    text: '二级单位'
  },
  3: {
    color: 'cyan',
    text: '三级单位'
  }
}

export default {
  name: 'DeptList',
  components: {
    STable,
    AddDept,
    EditDept,
    RemoveDept
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 部门对象
      deptFathers: [],
      // 表头
      columns: [
        {
          title: '部门名称',
          dataIndex: 'deptName',
          align: 'center'
        },
        {
          title: '上级单位',
          dataIndex: 'deptFather',
          scopedSlots: { customRender: 'deptFather' },
          align: 'center'
        },
        {
          title: '部门类型',
          dataIndex: 'deptType',
          scopedSlots: { customRender: 'deptType' },
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收
        const queryJson = { 'queryParam': JSON.stringify(this.queryParam) }
        return queryDept(Object.assign(parameter, queryJson))
          .then(res => {
            return res.data.result
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true
    }
  },
  filters: {
    deptTypeTextFilter (type) {
      return deptTypeMap[type].text
    },
    deptTypeColorFilter (type) {
      return deptTypeMap[type].color
    },
    deptFatherTextFilter (deptId) {
      let deptFatherName = ''
      for (const dept of that.deptFathers) {
        if (parseInt(dept.deptId) === parseInt(deptId)) {
          deptFatherName = dept.deptName
          break
        }
      }
      return deptFatherName
    }
  },
  beforeCreate () {
    that = this
  },
  created () {
    this.tableOption()
    listDept()
      .then(res => {
        if (res.data.message !== 'error') {
          this.deptFathers = res.data.result
        } else {
          this.$message.error('操作失败！')
        }
      })
  },
  methods: {
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleEdit (record) {
      this.$refs.editDept.edit(record)
    },
    handleOk () {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>
